package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dtos.UserRelationDto;
import com.heima.model.behavior.pojos.UserBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.UserFollowMapper;
import com.heima.user.service.ApUserFanService;
import com.heima.user.service.ApUserFollowService;
import com.heima.user.service.ApUserService;
import com.heima.user.service.UserFollowService;
import com.heima.utils.thread.ApUserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.Date;
import java.util.Objects;

/**
 * @author Dikk
 */
@Service
public class UserFollowServiceImpl extends ServiceImpl<UserFollowMapper, UserBehavior> implements UserFollowService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private ApUserService userService;

    @Autowired
    private IWemediaClient wemediaClient;

    @Autowired
    private ApUserFanService apUserFanService;

    @Autowired
    private ApUserFollowService apUserFollowService;

    /**
     * 是否关注作者
     *
     * @param userRelationDto
     * @return
     */
    @Override
    public ResponseResult operation(UserRelationDto userRelationDto) {

        //校验参数
        if (Objects.isNull(userRelationDto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //拿到User对象
        ApUser user = ApUserThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //根据key从缓存中获取UserBehavior对象
        Object object = cacheService.hGet(user.getId().toString(), userRelationDto.getArticleId().toString());
        UserBehavior userBehavior = JSON.parseObject(object.toString(), UserBehavior.class);
        if (object == null) {
            userBehavior = new UserBehavior();
        }
        userBehavior.setIsFollow(userRelationDto.getOperation() == 0 ? true : false);
        cacheService.hPut(user.getId().toString(), userRelationDto.getArticleId().toString(), JSON.toJSONString(userBehavior));

        //判断是操作
        if (userRelationDto.getOperation() == 0) {
            //关注操作
            //生成用户粉丝表数据
            ApUserFan apUserFan = new ApUserFan();
            apUserFan.setUserId(userRelationDto.getAuthorId());
            apUserFan.setFansId(user.getId());
            apUserFan.setFansName(user.getName());
            apUserFan.setIsDisplay((short) 0);
            apUserFan.setIsShieldComment((short) 0);
            apUserFan.setIsShieldLetter((short) 0);
            apUserFan.setLevel((short) 0);
            apUserFan.setCreatedTime(new Date());

            apUserFanService.save(apUserFan);
            //生成用户关注数据
            ApUserFollow apUserFollow = new ApUserFollow();
            apUserFollow.setUserId(user.getId());
            apUserFollow.setFollowId(userRelationDto.getAuthorId());
            //通过authorId，远程调用wmUser数据

            WmUser wmUser = wemediaClient.obtainWmUser(userRelationDto.getAuthorId());
            ApUser authorUser = userService.getById(wmUser.getApUserId());

            apUserFollow.setFollowName(authorUser.getName());
            apUserFollow.setCreatedTime(new Date());
            apUserFollow.setIsNotice((short) 0);
            apUserFollow.setLevel((short) 0);

            apUserFollowService.save(apUserFollow);
        } else if (userRelationDto.getOperation() == 1) {
            //取消关注 把用户和粉丝数据表数据
            //删除粉丝表数据
            apUserFanService.remove(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getUserId, userRelationDto.getAuthorId()));

            //删除关注表数据
            apUserFollowService.remove(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getUserId, user.getId()));

        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
